package com.maoxx.stuchoosemssbv_service.controller;

import com.maoxx.stuchoosemssbv_service.entity.Student;
import com.maoxx.stuchoosemssbv_service.entity.Teacher;
import com.maoxx.stuchoosemssbv_service.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @className: TeacherController
 * @author: Maoxx
 * @date: 2024/11/2 20:27
 * @Version: 1.0
 * @description:
 */


@RestController
@RequestMapping("/teacher")
@CrossOrigin(origins = "*")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @PostMapping("/login")
    public boolean login(@RequestBody Teacher teacher) {
        Teacher byId = teacherService.findById(teacher.getTid());
        if (byId != null && byId.getPassword().equals(teacher.getPassword())) {
            return true;
        } else {
            return false;
        }
    }


    @GetMapping("/findById/{tid}")
    public Teacher findById(@PathVariable("tid") Integer tid) {
        return teacherService.findById(tid);
    }

    @GetMapping("/findAll")
    public List<Teacher> findAll() {
        return teacherService.findAll();
    }

    @PutMapping("/updateTeacher")
    public boolean updateTeacher(@RequestBody Teacher teacher) {
        return teacherService.updateById(teacher);
    }

    @GetMapping("/findByPage/{page}/{size}")
    public List<Teacher> findByPage(@PathVariable Integer page, @PathVariable Integer size) {
        return teacherService.findByPage(page, size);
    }

    @PostMapping("/addTeacher")
    public boolean addTeacher(@RequestBody Teacher teacher) {
        return teacherService.save(teacher);
    }

    @DeleteMapping("/deleteById/{tid}")
    public boolean deleteById(@PathVariable("tid") Integer tid) {
        return teacherService.removeById(tid);
    }
}
